﻿using Coldairarrow.Business.TD;
using Coldairarrow.Entity.TD;
using Coldairarrow.IBusiness.DTO;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Coldairarrow.IBusiness;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;

namespace Coldairarrow.Api.Controllers.TD
{
    [Route("/TD/[controller]/[action]")]
    public class TD_AllocateController : BaseApiController
    {
        #region DI

        public TD_AllocateController(ITD_AllocateBusiness tD_AllocateBus, IOperator op)
        {
            _tD_AllocateBus = tD_AllocateBus;
            _Op = op;
        }

        IOperator _Op { get; }
        ITD_AllocateBusiness _tD_AllocateBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<TD_AllocateDTO>> GetDataList(TD_AllocatePageInput input)
        {
            input.StorId = _Op.Property.DefaultStorageId;
            return await _tD_AllocateBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<TD_AllocateViewDTO> GetTheData(IdInputDTO input)
        {
            return await _tD_AllocateBus.GetTheDataAsync(input.id);
        }

        /// <summary>
        /// 调拨出入库列表信息
        /// </summary>
        /// <param name="cateqrcode">调拨单的二维码</param>
        /// <param name="status">出入库状态</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<AllocatePackDetailDTO>> GetAllocatePackListDataAsync(string cateqrcode, int status)
        {
            return await _tD_AllocateBus.GetAllocatePackListDataAsync(cateqrcode, status);
        }

        /// <summary>
        /// 调拨出入库获取包的信息
        /// </summary>
        /// <param name="PackQrCode">包二维码</param>
        /// <param name="cateqrcode">调拨单的二维码</param>
        /// <param name="status">出入库状态</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AllocatePackDTO> GetAllocatePackData(string PackQrCode, string cateqrcode, int status)
        {
            return await _tD_AllocateBus.GetAllocatePackDataAsync(PackQrCode, cateqrcode, status);
        }

        /// <summary>
        /// 调拨出库
        /// </summary>
        /// <param name="data"></param>
        /// 增加混合包和单个物料调拨  2022-02-23  赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task UpcateOutStorage(AllocateDTO data)
        {
            await _tD_AllocateBus.UpcateOutStorageAsync(data);
        }

        /// <summary>
        /// 调拨出库获取单个成品物料信息
        /// </summary>
        /// <param name="JobQrCode">成品二维码</param>
        /// <param name="cateqrcode">调拨单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AllocateJobMaterialDTO> GetAllocateJobMaterialData(string JobQrCode, string cateqrcode)
        {
            return await _tD_AllocateBus.GetAllocateJobMaterialDataAsync(JobQrCode, cateqrcode);
        }

        /// <summary>
        /// 调拨出入库单个物料
        /// </summary>
        /// <param name="data"></param>
        /// 2022-02-23   赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task<AllocateMaterialDTO> GetAllocateDetailMaterialAsync(AllocateDTO data)
        {
            return await _tD_AllocateBus.GetAllocateDetailMaterialAsync(data);
        }

        /// <summary>
        /// 调拨单出库移除
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteAllocateDetailAsync(AllocatePackDetailDTO data)
        {
            await _tD_AllocateBus.DeleteAllocateDetailAsync(data);
        }

        /// <summary>
        /// 调拨出库提交
        /// </summary>
        /// <param name="cateqrcode">调拨单的二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitcateOutStorage(string cateqrcode)
        {
            await _tD_AllocateBus.SubmitcateOutStorageAsync(cateqrcode);
        }

        /// <summary>
        /// 打印调拨单
        /// </summary>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<TD_AllocateListInfo> GetAllocateListInfoPrint(string ParentId)
        {
            var res = await _tD_AllocateBus.GetAllocateListInfoPrint(ParentId);
            return res;
        }

        /// <summary>
        /// 调拨入库
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpcateInStorage(AllocateDTO data)
        {
            await _tD_AllocateBus.UpcateInStorageAsync(data);
        }

        /// <summary>
        /// 调拨入库获取单个成品物料信息
        /// </summary>
        /// <param name="JobQrCode">成品二维码</param>
        /// <param name="cateqrcode">调拨单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AllocateJobMaterialDTO> GetInAllocateJobMaterialData(string JobQrCode, string cateqrcode)
        {
            return await _tD_AllocateBus.GetInAllocateJobMaterialDataAsync(JobQrCode, cateqrcode);
        }

        /// <summary>
        /// 调拨单入库移除
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteAllocateWarehousing(AllocatePackDetailDTO data)
        {
            await _tD_AllocateBus.DeleteAllocateWarehousingAsync(data);
        }

        /// <summary>
        /// 调拨入库提交
        /// </summary>
        /// <param name="cateqrcode">调拨单的二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitcateInStorage(string cateqrcode)
        {
            await _tD_AllocateBus.SubmitcateInStorageAsync(cateqrcode);
        }

        /// <summary>
        /// 获取调拨入库的仓库货位
        /// </summary>
        /// <param name="storId">仓库id</param>
        /// <param name="locationqrcode">货位二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_Location> GetLocation(string storId, string locationqrcode)
        {
            return await _tD_AllocateBus.GetLocation(storId, locationqrcode);
        }

        #endregion

        #region 提交
        /// <summary>
        /// 添加/修改调拨单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveData(TD_Allocate data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);
                data.Amount = 0;
                data.AllocateNum = 0;
                foreach (var item in data.AllocateDetails)
                {
                    InitEntity(item);
                    item.AllocateId = data.Id;
                    // item.ToStorId = data.ToStorId;
                    //data.Amount += item.Price * item.AllocateNum;
                    //item.Amount = item.Price * item.AllocateNum;
                    data.AllocateNum += item.AllocateNum;
                    //data.StorId = item.FromStorId;
                }
                await _tD_AllocateBus.AddDataAsync(data);
            }
            else
            {
                data.Amount = 0;
                foreach (var item in data.AllocateDetails)
                {
                    if (item.Id.StartsWith("newid_"))
                        InitEntity(item);
                    item.AllocateId = data.Id;
                    //item.ToStorId = data.ToStorId;
                    //data.Amount += item.Price * item.AllocateNum;
                    
                }
                await _tD_AllocateBus.UpdateDataAsync(data);
            }
        }
        /// <summary>
        /// 删除调拨单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _tD_AllocateBus.DeleteDataAsync(ids);
        }

        /// <summary>
        /// 调拨单审批
        /// </summary>
        /// <param name="audit"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task Audit(AuditDTO audit)
        {
            audit.StorId = _Op.Property.DefaultStorageId;
            audit.AuditUserId = _Op.UserId;
            audit.AuditTime = DateTime.Now;
            if (audit.AuditType == AuditType.Approve)
                await _tD_AllocateBus.Approve(audit);
            else if (audit.AuditType == AuditType.Confirm) //开始作业
                await _tD_AllocateBus.StartWork(audit);
            else
                await _tD_AllocateBus.Reject(audit);
        }
        #endregion
    }
}